from copy import copy
import random

from sqlalchemy import Table

from app.config.config import config
from app.settings import env
from common.database import db_connect

db_session, Base, engine = db_connect()


class User(Base):
    # 表结构的反射加载
    __table__ = Table('user', Base.metadata, autoload_with=engine)

    def get_one(self):
        return db_session.query(User).first()

    def find_by_username(self, username):
        return db_session.query(User).filter_by(username=username).all()

    def do_register(self, username, password):
        nickname = username.split('@')[0]
        picture_num = random.randint(1, 539)
        picture = str(picture_num) + '.jpg'
        job = '未定义'
        user = User(
            username=username,
            password=password,
            nickname=nickname,
            picture=picture,
            job=job
        )
        db_session.add(user)
        db_session.commit()
        return user

    def find_by_userid(self, user_id):
        user_info = db_session.query(User).filter_by(user_id=user_id).first()
        result = copy(user_info)
        result.picture = config[env].user_header_image_path + user_info.picture
        return result

        # user_info = db_session.query(User).filter_by(user_id=user_id).first()
        # image_prefix = config[env].user_header_image_path
        # if not user_info.picture.startswith(image_prefix):
        #     user_info.picture = image_prefix + user_info.picture
        # return user_info
